package com.jason.accountmd.service;

import com.jason.accountmd.dao.AccountDao;
import com.jason.accountmd.mapper.AccountMapper;
import com.jason.accountmd.service.localacc.ILocAccountServiceApi;
import com.jason.common.dubboapi.IAccountServiceApi;
import io.seata.rm.tcc.api.BusinessActionContext;
import io.seata.rm.tcc.api.BusinessActionContextParameter;
import org.apache.dubbo.config.annotation.Service;
import org.apache.shardingsphere.api.hint.HintManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service(retries = 0,timeout = 3000)
public class AccountService implements IAccountServiceApi {
    private static final Logger logger = LoggerFactory.getLogger(AccountService.class);
    @Autowired
    private AccountDao accountDao;

    @Autowired
    private ILocAccountServiceApi locAccountServiceApi;

    @Autowired
    private AccountMapper accountMapper;


    @Override
    public String addAmount(BusinessActionContext businessActionContext, @BusinessActionContextParameter Long accId, BigDecimal amount) {
        logger.info("=======================outer add===================");
        try {
            locAccountServiceApi.addAmount(null,accId+1,amount);
            accountDao.addAmount( businessActionContext,  accId,  amount);
            logger.info("给"+accId+"加"+amount);
            logger.info("=======================outer add end===================");
        }catch (Exception e) {
            logger.error(e.getMessage(),e);
            throw e;
        }
        return "success";
    }

    @Override
    public boolean commitAccount(BusinessActionContext businessActionContext) {
        logger.info("=======================out commit acc===================");
        try {
            Long accId = (Long) businessActionContext.getActionContext("accId");
            HintManager.clear();
            HintManager hintManager = HintManager.getInstance();
            hintManager.addDatabaseShardingValue("t_trans_atmo", accId);
            int result =accountMapper.updateConfirmTransAtmo(businessActionContext.getXid(),businessActionContext.getBranchId());
            boolean ret = result==1?true:false;
            logger.info("直接提交"+ret);
            logger.info("=======================out commit end==================="+ret);
            HintManager.clear();
        }catch (Exception e) {
            logger.error(e.getMessage(),e);
            return false;
        }
        return true;
    }

    @Override
    public boolean rollbackAccount(BusinessActionContext businessActionContext) {
        logger.info("=======================outer roll begin===================");
        Long accId = (Long) businessActionContext.getActionContext("accId");
        BigDecimal amount = new BigDecimal( businessActionContext.getActionContext("amount")+"");
        boolean result = true;
        try {
            accountDao.reduceAmount(businessActionContext,accId,amount);
        }catch (Exception e)  {
            result = false;
            logger.error(e.getMessage(),e);
        }
        logger.info("=======================outer roll end==================="+result);
        return result;
    }
}
